package cn.com.yto56.coresystem.business.msg.mc.biz.impl;

import cn.com.yto56.coresystem.business.msg.mc.biz.IClientInfoBiz;
import cn.com.yto56.coresystem.common.msg.util.base.StringUtils;
import cn.com.yto56.coresystem.common.msg.util.data.DataTypeChangeTool;
import cn.com.yto56.coresystem.common.msg.util.db.IPage;
import cn.com.yto56.coresystem.common.msg.util.db.QueryPage;
import cn.com.yto56.coresystem.common.msg.util.domain.AppResult;
import cn.com.yto56.coresystem.logic.msg.mc.dao.IClientInfoDao;
import cn.com.yto56.coresystem.logic.msg.mc.domain.ClientInfoBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by wangrui on 2017/5/4.
 */
@Service
public class ClientInfoBizImpl implements IClientInfoBiz {
    @Autowired
    private IClientInfoDao clientInfoDao;

    @Transactional
    @Override
    public AppResult addOrUpdateClientInfo(String sysInfo_Id, String userId, String onlineState, String connctionTime, String loginTime, String unConnectionTime, String clientType) {
        AppResult appResult = new AppResult();
        boolean resultFlag = false;
        String resultMessage = "执行失败";
        Map datas = null;
        if (StringUtils.isBlank(sysInfo_Id)) {
            resultMessage = "参数[yto_msg_sysInfo_Id]不能为空";
        } else if (StringUtils.isBlank(userId)) {
            resultMessage = "参数[yto_msg_userId]不能为空";
        } else {
            ClientInfoBean where = new ClientInfoBean();
            where.setYto_msg_sysinfo_id(sysInfo_Id);
            where.setYto_msg_userid(userId);
            ClientInfoBean set = new ClientInfoBean();
            set.setConnectiontime(connctionTime);
            set.setLogintime(loginTime);
            set.setClienttype(clientType);
            set.setOnlinestate(onlineState);
            set.setUnconnectiontime(unConnectionTime);
            IPage<ClientInfoBean> clientInfoBeanIPage = clientInfoDao.search(where, new QueryPage(1, 1));
            List<ClientInfoBean> clientInfoBeanList = (List<ClientInfoBean>) clientInfoBeanIPage.getRows();
            if (clientInfoBeanList != null && clientInfoBeanList.size() > 0) {
                if (onlineState.equals("1")) {
                    set.setConnectionnum(clientInfoBeanList.get(0).getConnectionnum() + 1);
                    set.setLoginnum(clientInfoBeanList.get(0).getLoginnum() + 1);
                }
                set.setUpdate_time(new Date());
                int t = clientInfoDao.update(set, where);
                if (t > 0) {
                    resultFlag = true;
                    resultMessage = "执行成功";
                }
            } else {
                set.setYto_msg_sysinfo_id(sysInfo_Id);
                set.setYto_msg_userid(userId);
                set.setConnectionnum(1);
                set.setLoginnum(1);
                set.setCreate_time(new Date());
                int t = clientInfoDao.add(set);
                if (t > 0) {
                    resultFlag = true;
                    resultMessage = "执行成功";
                }
            }
        }


        appResult.setResultFlag(resultFlag);
        appResult.setResultMessage(resultMessage);
        appResult.setDatas(datas);
        return appResult;
    }

    @Override
    public AppResult addClientInfo(String sysInfo_Id, String userId, String connctionTime, String loginTime) {
        AppResult appResult = new AppResult();
        boolean resultFlag = false;
        String resultMessage = "";
        Map datas = null;
        if (StringUtils.isBlank(sysInfo_Id)) {
            resultMessage = "参数[yto_msg_sysInfo_Id]不能为空";
        } else if (StringUtils.isBlank(userId)) {
            resultMessage = "参数[yto_msg_userId]不能为空";
        } else {
            ClientInfoBean clientInfoBean = new ClientInfoBean();
            clientInfoBean.setYto_msg_sysinfo_id(sysInfo_Id);
            clientInfoBean.setYto_msg_userid(userId);
            if (clientInfoDao.searchForUpdate(clientInfoBean)) {
                //插入
                clientInfoBean.setConnectiontime(connctionTime);
                clientInfoBean.setLogintime(loginTime);
                clientInfoBean.setConnectionnum(1);
                clientInfoBean.setLoginnum(1);
                resultFlag = true;
                resultMessage = "插入成功";
            } else {
                resultMessage = "数据库中存在该记录，插入失败";
            }
        }
        appResult.setResultFlag(resultFlag);
        appResult.setResultMessage(resultMessage);
        appResult.setDatas(datas);
        return appResult;
    }

    @Transactional
    @Override
    public AppResult updateClientInfo(String sysInfo_Id, String userId, String connctionTime, String loginTime, String clientType) {
        AppResult appResult = new AppResult();
        boolean resultFlag = false;
        String resultMessage = "执行失败";
        Map datas = null;
        ClientInfoBean where = new ClientInfoBean();
        where.setYto_msg_sysinfo_id(sysInfo_Id);
        where.setYto_msg_userid(userId);
        IPage<ClientInfoBean> clientInfoBeanIPage = clientInfoDao.search(where, new QueryPage(1, 1));
        List<ClientInfoBean> clientInfoBeanList = (List<ClientInfoBean>) clientInfoBeanIPage.getRows();
        if (clientInfoBeanList != null && clientInfoBeanList.size() > 0) {
            ClientInfoBean set = new ClientInfoBean();
            set.setConnectiontime(connctionTime);
            set.setLogintime(loginTime);
            set.setClienttype(clientType);
            set.setConnectionnum(clientInfoBeanList.get(0).getConnectionnum() + 1);
            set.setLoginnum(clientInfoBeanList.get(0).getLoginnum() + 1);
            int t = clientInfoDao.update(set, where);
            if (t > 0) {
                resultFlag = true;
                resultMessage = "执行成功";
            }
        } else {
            resultMessage = "数据库中不存在该记录";
        }

        appResult.setResultFlag(resultFlag);
        appResult.setResultMessage(resultMessage);
        appResult.setDatas(datas);
        return appResult;
    }


    @Override
    public AppResult getClientInfo(String sysInfo_Id, String userId) {
        AppResult appResult = new AppResult();
        boolean resultFlag = false;
        String resultMessage = "执行失败";
        List datas = null;
        ClientInfoBean where = new ClientInfoBean();
        where.setYto_msg_sysinfo_id(sysInfo_Id);
        where.setYto_msg_userid(userId);
        IPage<ClientInfoBean> clientInfoBeanIPage = clientInfoDao.search(where, new QueryPage(20, 1));
        List<ClientInfoBean> clientInfoBeanList = (List<ClientInfoBean>) clientInfoBeanIPage.getRows();
        if (clientInfoBeanList != null && clientInfoBeanList.size() > 0) {
            for (int i = 0; i < clientInfoBeanList.size(); i++) {
                datas.add(DataTypeChangeTool.beanToMap(clientInfoBeanList.get(i)));
            }
            resultFlag = true;
            resultMessage = "查询成功";
        }
        appResult.setResultFlag(resultFlag);
        appResult.setResultMessage(resultMessage);
        appResult.setDatas(datas);
        return appResult;
    }
}
